package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbGroupActive;
import com.bwie.service.TbGroupActiveService;
import com.bwie.mapper.TbGroupActiveMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@Service
public class TbGroupActiveServiceImpl extends ServiceImpl<TbGroupActiveMapper, TbGroupActive>
    implements TbGroupActiveService{
    @Autowired
    TbGroupActiveMapper tbGroupActiveMapper;
    @Autowired
    HttpServletRequest request;

    /**
     * 获取拼团活动分页列表
     * @param pageInfoVo
     * @return
     */
    @Override
    public R getGroupActivePage(PageInfoVo pageInfoVo) {
        Page<TbGroupActive> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());

        LambdaQueryWrapper<TbGroupActive> wrapper = new LambdaQueryWrapper<>();

        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.like(TbGroupActive::getActiveName,pageInfoVo.getKeyWord());
        }

        Page<TbGroupActive> tbGroupActivePage = tbGroupActiveMapper.selectPage(page, wrapper);
        return R.OK(tbGroupActivePage);
    }

    /**
     * 添加拼团活动
     * @param tbGroupActive
     * @return
     */
    @Override
    public R addGroupActive(TbGroupActive tbGroupActive) {
        String token = request.getHeader("token");
        if(token == null){
            return R.ERROR(404,"请先登录");
        }


        TbGroupActive groupActive = tbGroupActiveMapper.selectOne(
                new LambdaQueryWrapper<TbGroupActive>().eq(TbGroupActive::getActiveName, tbGroupActive.getActiveName())
        );
        if(groupActive != null){
            return R.ERROR(500,"该拼团活动已经存在");
        }

        long begin = tbGroupActive.getBeginTime().getTime();
        long end = tbGroupActive.getEndTime().getTime();
        if(begin>end){
            R.ERROR(500,"开始时间不能晚于截止时间");
        }

        tbGroupActive.setCreateBy("系统");
        tbGroupActive.setCreateTime(new Date());

        tbGroupActiveMapper.insert(tbGroupActive);

        return R.OK();
    }

    @Override
    public R updateGroupActive(TbGroupActive tbGroupActive) {
        String token = request.getHeader("token");
        if(token == null){
            return R.ERROR(404,"请先登录");
        }


        long begin = tbGroupActive.getBeginTime().getTime();
        long end = tbGroupActive.getEndTime().getTime();
        if(begin>end){
            R.ERROR(500,"开始时间不能晚于截止时间");
        }
        tbGroupActive.setUpdateBy("系统");
        tbGroupActive.setUpdateTime(new Date());

        tbGroupActiveMapper.updateById(tbGroupActive);

        return R.OK();
    }

    @Override
    public R deleteGroupActive(Integer activeId) {
        TbGroupActive tbGroupActive = tbGroupActiveMapper.selectById(activeId);
        if(tbGroupActive == null){
            return R.ERROR(404,"该拼团活动不存在");
        }
        tbGroupActiveMapper.deleteById(activeId);
        return R.OK();
    }
}




